/* Start out defining command objects
	used to decouple the toggle class
	from the actual implementation of
	each toggle.
*/

// Command Object for Editor Toggle
var editor_command = {
	on: function() {
		Admin_Editor.on();
		Admin.setData('editor', 1);
	},
	off: function() {
		Admin_Editor.off();
		Admin.setData('editor', 0);
	}
}

// Command Object for Confirm Toggle
var confirm_command = {
	on: function() {
		Admin_Popup.on();
		Admin.setData('popup', 1);
	},
	off: function() {
		Admin_Popup.off();
		Admin.setData('popup', 0);
	}
}

// Admin Singleton - where all the admin magic happens
var Admin = {
	
	// Function to update session data for toggle states
	setData: function(setting, value) {	
		var obj = { 'ajax':true, 'setting':setting, 'value':value };
		var params = Object.toQueryString(obj);
		var xhr = new XHR({ method:'post' });
		xhr.send(BASE_URL+'admin/setSessData/', params);
	},
	
	// Admin init function
	init : function() {
		// Get toggles from last page
		editor_default = EDIT_DEFAULT;
		popup_default = POP_DEFAULT;
		toggle_default = TOG_DEFAULT;
		
		// Get all inline editable fields
		Admin_Editor.instances = Admin_Editor.instances();
		
		// Admin box toggle effect
		var slider = new Fx.Slide('admin_body', {
			duration: 500,
			onComplete: function(e) {
				if (e.hasClass('hide'))
					$('admin_handle').setHTML('&or;');
				else
					$('admin_handle').setHTML('&and;');
				e.toggleClass('hide');
				Admin.setData('toggle', Number(e.hasClass('hide')) );
			}
		});
		
		// Create toggle links
		var edit_toggle_link = $('evt_toggle');
		var edit_toggle = new Toggle(edit_toggle_link, editor_command);
		
		var confirm_toggle_link = $('conf_toggle');
		var confirm_toggle = new Toggle(confirm_toggle_link, confirm_command);
		
		
		// Set toggles based on session
		if(!editor_default)
			edit_toggle.toggle();
		else
			edit_toggle.on();
			
		if(!popup_default)
			confirm_toggle.toggle();
		else
			confirm_toggle.on();
		
		if(!toggle_default) {
			$('admin_body').removeClass('hide');
			$('admin_handle').setHTML('&or;');
			slider.hide();
		}
		
		// Event for admin box title bar toggle
		$('admin_title').addEvent('click', function() {
			slider.toggle();
		});

		//Prevents that annoying loading flicker
		$('admin_body').removeClass('hidden');
	}
};

window.addEvent('domready', Admin.init);